package com.longfor.dragonshard.service.impl;

import com.longfor.dragonshard.common.enums.BizEnum;
import com.longfor.dragonshard.dto.account.LoginDTO;
import com.longfor.dragonshard.entity.Demo;
import com.longfor.dragonshard.entity.DemoAccount;
import com.longfor.dragonshard.mapper.DemoAccountMapper;
import com.longfor.dragonshard.mapper.DemoMapper;
import com.longfor.dragonshard.service.IDemoAccountService;
import com.longfor.dragonshard.service.IDemoService;
import com.longfor.dragonshard.service.base.AbstractBaseService;
import com.longfor.drogon.common.LFConstants;
import com.longfor.drogon.common.exception.LFBizException;
import com.longfor.drogon.common.util.LFDefaultKeyGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * Demo Service类
 *
 * @author chi.zhang
 * @version v1.0
 * @create 2017-10-05 下午2:28
 **/
@Service
@CacheConfig(cacheNames = LFConstants.CACHE.DEFAULT_CACHE_NAME)
public class DemoAccountServiceImpl extends AbstractBaseService<DemoAccount> implements IDemoAccountService {
    private Logger logger = LoggerFactory.getLogger(DemoAccountServiceImpl.class);

    @Autowired
    private DemoAccountMapper demoAccountMapper;

    @Override
    @Cacheable(key = "#p0")
    public Optional<DemoAccount> selectById(Long id) {
        return Optional.ofNullable(demoAccountMapper.selectByPrimaryKey(id));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insert(DemoAccount demoAccount) {
        LFDefaultKeyGenerator defaultKeyGenerator = new LFDefaultKeyGenerator();
        demoAccount.setId(defaultKeyGenerator.generateKey());
        demoAccountMapper.insertSelective(demoAccount);

        //todo 更多数据填充

        return demoAccount.getId();
    }

    @Override
    @CachePut(key = "#p0.id")
    public void update(DemoAccount demoAccount) {
        demoAccountMapper.updateByPrimaryKeySelective(demoAccount);
    }

    @Override
    @CacheEvict(key = "#p0")
    public Integer delete(Long id) {
        return demoAccountMapper.deleteByPrimaryKey(id);
    }


}
